home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-01
/
wtek0693.zip
/
ECKEL.ZIP
/
MULTIMED.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1992-07-15
|
5KB
|
197 lines
//: MULTIMED.CPP -- MultiMedia class implementation.
//. This uses the high-level MCI string interface, but
//. can be changed to use lower-level interfaces without
//. changing the rest of the code in the project.
#include <stdlib.h>
#include <string.h>
#include <strstream.h>
#include <windows.h>
#include "multimed.h"
#include <mmsystem.h>
CD cd; // single global instance
// Error checking MCI interface function:
static char* mci_command(char* cmd) {
const size = 200;
static char returnbuf[size]; // for mci return message
int error = mciSendString(cmd, returnbuf, size, NULL);
// Return string is in returnbuf.
if(error) {
MessageBox(NULL, cmd, "MCI command causing error", MB_OK);
char errbuf[size];
if(mciGetErrorString(error, errbuf, size))
MessageBox(NULL, errbuf, "MCI error", MB_OK);
else
MessageBox(NULL, "Unknown error", "MCI error", MB_OK);
}
return returnbuf;
}
int CD::open() {
if(!cd_open) {
char * isthere = mci_command("capability cdaudio can play");
if(strcmp("true", isthere) == 0) present = 1; // disk available
if(!present) return 0;
mci_command("open cdaudio shareable");
mci_command("set cdaudio time format tmsf");
maxtracks = atoi(mci_command("status cdaudio number of tracks"));
length = new songlength[maxtracks];
for(int i = 1; i <= maxtracks; i++) {
ostrstream commandstr(command, sz);
commandstr << "status cdaudio length track " << i << '\0';
length[i-1].minutes = atoi(strtok(mci_command(command), ":"));
length[i-1].seconds = atoi(strtok(NULL, ":"));
}
cd_open = 1;
}
return 1;
}
void CD::play() {
open();
if(!present) return;
state = normal;
ostrstream commandstr(command, sz);
// Note format: track:minutes:seconds:frames
commandstr << "play cdaudio from " <<
track << ":" << minute << ":"<<
second << ":00" << '\0';
mci_command(command);
}
CD::CD(when w) : state(normal), cd_open(0), maxtracks(0),
length(0), track(1), minute(0), second(0), present(0) {
if(w == open_now) open();
}
CD::~CD() {
if(cd_open) mci_command("close cdaudio");
delete []length; // dynamically allocated buffer
}
void CD::stop() {
if(!cd_open) open();
if(!present) return;
mci_command("stop cdaudio");
track = 1; minute = second = 0;
state = normal;
}
void CD::forward_track() {
if(++track > maxtracks) track = maxtracks;
minute = second = 0;
play();
}
void CD::backward_track() {
if(--track < 1) track = 1;
minute = second = 0;
play();
}
void CD::update_position() {
// Change internal data to reflect current CD position
if(!present) return;
track = atoi(strtok(mci_command("status cdaudio position"), ":"));
minute = atoi(strtok(NULL, ":"));
second = atoi(strtok(NULL, ":"));
}
void CD::rewind() {
// Rewind:
open();
if(!present) return;
update_position();
state = frewind;
}
void CD::rewind_step() {
if(--second < 0) {
if(--minute < 0) {
if(--track < 1) {
track = 1;
minute = second = 0;
} else {
minute = length[track - 1].minutes;
second = length[track - 1].seconds;
}
} else {
second = 59;
}
}
}
void CD::fast_forward() {
// Fast-forward:
open();
if(!present) return;
update_position();
state = fforward;
}
void CD::fforward_step() {
if(minute == length[track-1].minutes) {
if(++second >= length[track-1].seconds)
if(++track > maxtracks) {
track = maxtracks;
minute = length[track-1].minutes;
second = length[track-1].seconds;
} else {
minute = second = 0;
}
return;
}
// Here, we aren't at the track's minute length
if(++second >= 60) {
minute++;
second = 0;
}
}
////////// SoundPlayer Method Implementations ////////////
void SoundPlayer::play(char * file) {
sndPlaySound(file, SND_ASYNC);
}
SoundRecorder::SoundRecorder() : audio_used(0), recording(0) {
// You must have the empty sound file tmp.wav
// in the current directory
mci_command("open tmp.wav type waveaudio");
mci_command("cue tmp.wav input");
mci_command("delete tmp.wav from 0");
}
SoundRecorder::~SoundRecorder() {
stop_recording();
mci_command("close tmp.wav");
}
void SoundRecorder::start_recording() {
char *ready = mci_command("status tmp.wav ready");
if(strcmp(ready, "true") != 0) {
MessageBox(NULL, ready, "tmp.wav not ready", MB_OK);
return;
}
if(audio_used) {
mci_command("delete tmp.wav from 0");
}
mci_command("record tmp.wav from 0");
audio_used = recording = 1;
}
void SoundRecorder::stop_recording() {
if(recording) {
mci_command("stop tmp.wav");
recording = 0;
}
}
void SoundRecorder::save(char * file) {
stop_recording();
ostrstream commandstr(command, sz);
commandstr << "save tmp.wav " << file << '\0';
mci_command(command);
}